package com.demo.java8Stream;

/**
 * java8 List根据某个字段去重
 */
public class streamDistinct {

    /**
     *1、使用toCollection和TreeSet去重
     * TreeSet内部使用的是TreeMap，使用指定Comparator比较元素，如果元素相同，则新元素代替旧元素。
     */
   /* List<TalentPlanStudentEntity> studentList = relatePlanStudentList.stream()
            .collect(Collectors.collectingAndThen(Collectors.toCollection(
                    () -> new TreeSet<>(Comparator.comparing(TalentPlanStudentEntity::getUserId))), ArrayList::new));

*/

    /**
     * 2、使用Collectors.toMap去重
     * Collectors.toMap需要使用三个参数的版本，前两个参数一个是keyMapper函数一个是valueMapper函数的，
     * 第三个参数BinaryOperator函数接口。BinaryOperator函数接收两个参数，一个oldValue，一个newValue。用于当key重复时的数据处理
     */

/*    List<TalentPlanStudentEntity> studentList = new ArrayList<>(relatePlanStudentList.stream()
            .collect(Collectors.toMap(TalentPlanStudentEntity::getUserId, Function.identity(), (oldValue, newValue) -> oldValue))
            .values());*/

    /**
     * 例子
     */
/*

    List<User> userList = new ArrayList<>();

        userList.add(new User("1","李大锤","23","南京"));
        userList.add(new User("2","张无忌","18","西安"));
        userList.add(new User("3","刘德华","26","苏州"));
        userList.add(new User("4","郭靖","33","上海"));

        userList.add(new User("1","李大锤","23","南京"));    //id相同，其他数据也相同
        userList.add(new User("3","带头大哥","36","杭州"));  //id相同，其他数据不同

        System.out.println(userList);
    //根据userid去重
    userList = userList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new       TreeSet<>(Comparator.comparing(User :: getUserid))), ArrayList::new));
*/


}
